﻿Imports SIRData
Public Class frmNotaDeIngresoListado

  Private Const c_NotaIngreso As Int16 = 1

  Private m_bKey As Boolean = False

  Private Sub frmNotaDeIngresoListado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try

      Call ListarNotaIngresoSalida(True)

      m_bKey = True ' Establecer la llave

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub ListarNotaIngresoSalida(ByVal bTodos As Boolean)
    Dim m_NotaIngresoSalida As New clsNotaIngresoSalida(g_sConnectString)
    Try

      If bTodos Then
        dgvListado.DataSource = m_NotaIngresoSalida.NotaIngresoSalidaListar(c_NotaIngreso)
      Else
        dgvListado.DataSource = m_NotaIngresoSalida.NotaIngresoSalidaListar(c_NotaIngreso, dtpFechaIni.Value, dtpFechaFin.Value)
      End If


    Catch ex As Exception
      Throw New Exception(ex.Message)
    Finally
      m_NotaIngresoSalida = Nothing
      GC.Collect()
    End Try
  End Sub

  Private Sub dgvListado_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles dgvListado.RowPrePaint
    Try
      dgvListado.Rows(e.RowIndex).DefaultCellStyle.BackColor = IIf(e.RowIndex Mod 2 = 0, Color.Honeydew, Color.White)
      dgvListado.Rows(e.RowIndex).DefaultCellStyle.Font = New Font("Lucida Sans", 8)
    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub dgvListado_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvListado.CellContentClick
    Try
      'Esta parte me permite seleccionar solo un checkbox y se elimina el anterior 
      dgvListado.Rows(e.RowIndex).Cells("chkSel").Value = Not dgvListado.Rows(e.RowIndex).Cells("chkSel").Value
      For Each fila As DataGridViewRow In dgvListado.Rows
        If Not fila Is dgvListado.Rows(dgvListado.CurrentCell.RowIndex) Then
          fila.Cells("chkSel").Value = False
        End If
      Next

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub btnFiltrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFiltrar.Click
    Try
      If dtpFechaIni.Value.Date > dtpFechaFin.Value.Date Then
        MessageBox.Show("La fecha inicial debe ser menor o igual que la fecha final", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
        dtpFechaIni.Focus()
      Else
        Call ListarNotaIngresoSalida(False)
      End If

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
    Dim oForm As New frmNotaDeIngresoNuevo
    Try
      oForm.ShowDialog()

      Call ListarNotaIngresoSalida(True)

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click
    Dim oForm As New frmNotaDeIngresoEditar
    Try
      If dgvListado.Item("chkSel", dgvListado.CurrentCell.RowIndex).Value = False Then
        MessageBox.Show("Debe seleccionar con un check el registro que desea editar.", g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
      Else
        
        oForm.oiNumNota = dgvListado.Item("iNumNota", dgvListado.CurrentCell.RowIndex).Value
        oForm.ocNumDoc = dgvListado.Item("cNumDoc", dgvListado.CurrentCell.RowIndex).Value
        oForm.lbliNumNota.Text = dgvListado.Item("cNumDoc", dgvListado.CurrentCell.RowIndex).Value
        oForm.dtpsdFecDoc.Value = dgvListado.Item("sdFecDoc", dgvListado.CurrentCell.RowIndex).Value
        oForm.lblvNomAlmOri.Text = dgvListado.Item("vNomAlmOri", dgvListado.CurrentCell.RowIndex).Value
        oForm.lblvNomAlmOri.Tag = dgvListado.Item("siCodAlmOri", dgvListado.CurrentCell.RowIndex).Value
        oForm.lblvNomAlmDes.Text = dgvListado.Item("vNomAlmDes", dgvListado.CurrentCell.RowIndex).Value
        oForm.lblvNomAlmDes.Tag = dgvListado.Item("siCodAlmDes", dgvListado.CurrentCell.RowIndex).Value
        oForm.lblRecibido.Text = dgvListado.Item("vNomEmp", dgvListado.CurrentCell.RowIndex).Value
        oForm.lblRecibido.Tag = dgvListado.Item("siCodEmp", dgvListado.CurrentCell.RowIndex).Value
        oForm.oiNumOrdCom = dgvListado.Item("iNumDocRef", dgvListado.CurrentCell.RowIndex).Value

        oForm.bNuevo = False
        oForm.ShowDialog()

        Call ListarNotaIngresoSalida(True)
      End If

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

  Private Sub btnImprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImprimir.Click
    Try

    Catch ex As Exception
      MessageBox.Show(ex.Message, g_sModulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      GC.Collect()
    End Try
  End Sub

End Class